﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Web;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using Sherwood.Content.SearchClient;
using Sherwood.Content.Tests;
using Sherwood.Security;

namespace Sherwood.Samples.SearchClient
{
    /// <summary>
    /// Interaction logic for SolrXmlSearchWindow.xaml
    /// </summary>
    public partial class SolrXmlSearchWindow : Window
    {
        public SolrXmlSearchWindow()
        {
            InitializeComponent();

            url.Text = "http://localhost:57712/search.ashx?version=2.2&start=0&rows=50";
            client.Text = "SampleClient";
            privateKey.Text = SampleClient.PrivateKey;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var uriString = url.Text + "&q=" + HttpUtility.UrlEncode(search.Text);
            uriString = url.Text + "&q=" + search.Text.Replace(" ", "+"); //TEMP
            var uri = new Uri(uriString);
            var searchClient = new ClientSignedSearchClient(uri.GetLeftPart(UriPartial.Path), client.Text, privateKey.Text);
            var searchUrl = searchClient.GetPreparedSearchUrl(uriString.Split('?')[1]);
            //Process.Start(searchUrl);

            MessageBox.Show(new StreamReader(WebRequest.Create(searchUrl).GetResponse().GetResponseStream()).ReadToEnd());
        }
    }
}
